/**
 * Created by Administrator on 2016/8/3.
 */
"use strict";
swan_pc
    .controller("EventLookController",function($scope,$q,EventManage,$state,EventManageById,candidateUserByEvent,candidateUserManageById){
        //页面跳转到eventDetail展示
        $scope.stateToEventDetail=function(id){
            $scope.$parent.currentEventDetailId=id;
            $state.go("eventdetail");
        }
        //获取活动列表并分类展示
        $scope.myEventList=[];
        $scope.EventListShow=[];
        $scope.myEventListDraft=[];
        $scope.myEventListConfirm=[];
        $scope.init=function() {
            EventManage.query(function (result) {
                $scope.myEventList = result;
                angular.forEach($scope.myEventList, function (data, index, array) {
                    if(data.status=="DRAFT"){
                        $scope.myEventListDraft.push(data);
                    }else if(data.status=="CONFIRM"){
                        $scope.myEventListConfirm.push(data);
                    }
                });
                $scope.EventListShow=$scope.myEventList;
            }, function () {
            })
        }
        $scope.init();
        $scope.$watch(function(){
            return $scope.$parent.eventStatusShow;
        },function(){
            if($scope.$parent.eventStatusShow==2){
                $scope.EventListShow=$scope.myEventList;
            }else if($scope.$parent.eventStatusShow==3){
                $scope.EventListShow=$scope.myEventListDraft;
            }else if($scope.$parent.eventStatusShow==4){
                $scope.EventListShow=$scope.myEventListConfirm;
            }
        })
        //individualEventStatus展示
        $scope.eventStatusNameShow=function(option){
            var statusName=""
            if(option=="DRAFT"){
                statusName="草稿";
            }else if(option=="CONFIRM"){
                statusName="确认";
            }else if(option=="CANCEL"){
                statusName="取消";
            }else if(option=="CLOSED"){
                statusName="关闭";
            }
            return statusName;
        }
        //删除活动
        $scope.deleteEventById=function(eventId){
            console.log("deleteEventRun:"+eventId);
            $scope.candidateInCurrentEvent=[];
            var def=$q.defer();
            var pro=def.promise;
            candidateUserByEvent.query({id:eventId},function(result){
                $scope.candidateInCurrentEvent=result;
                var length=$scope.candidateInCurrentEvent.length;
                //console.log(length);
                if(length>0) {
                    angular.forEach($scope.candidateInCurrentEvent, function (data, index, arry) {
                        console.log("deleteCandidate:" + data.id);
                        candidateUserManageById.delete({id: data.id}, function (result) {
                            console.log("deleteSuccess");
                        }, function (result) {
                            console.log("deleteError");
                        })
                        if (index == length - 1) {
                            def.resolve();
                        }
                    })
                }else{
                    EventManageById.delete({id:eventId},function(result){
                        $scope.init();
                        swal("删除活动成功","","success");
                    },function(result){
                        swal("删除活动失败","","error");
                    })
                }
            })
            pro.then(
                function(){
                    EventManageById.delete({id:eventId},function(result){
                        $scope.init();
                        swal("删除活动成功","","success");
                    },function(result){
                        swal("删除活动失败","","error");
                    })
                }
            )
        }
        //eventList排序相关
        $scope.col="time";
        $scope.desc=0;
    })